﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.DocumentLabels
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class DocumentLabelInstruction:InstructionBase
    {
        #region props
        private string _SrcFolderPath;
        /// <summary></summary>
        public string SrcFolderPath
        {
            get
            {
                return this._SrcFolderPath;
            }
            set
            {
                this._SrcFolderPath = value;
            }
        }

        private string _FileExtension;
        /// <summary></summary>
        public string FileExtension
        {
            get
            {
                return this._FileExtension;
            }
            set
            {
                this._FileExtension = value;
            }
        }

        private int _DistanceThreshold;
        /// <summary></summary>
        public int DistanceThreshold
        {
            get
            {
                return this._DistanceThreshold;
            }
            set
            {
                this._DistanceThreshold = value;
            }
        }

        private int _SetenceDistance;
        /// <summary></summary>
        public int SetenceDistance
        {
            get
            {
                return this._SetenceDistance;
            }
            set
            {
                this._SetenceDistance = value;
            }
        }

        private int _ParagraphDistance;
        /// <summary></summary>
        public int ParagraphDistance
        {
            get
            {
                return this._ParagraphDistance;
            }
            set
            {
                this._ParagraphDistance = value;
            }
        }

        private string _EvidenceFilePath;
        /// <summary></summary>
        public string EvidenceFilePath
        {
            get
            {
                return this._EvidenceFilePath;
            }
            set
            {
                this._EvidenceFilePath = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public DocumentLabelInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._SrcFolderPath = string.Empty;
            this._FileExtension = string.Empty;
            this._DistanceThreshold = 25;
            this._ParagraphDistance = 10;
            this._SetenceDistance = 5;
            this._EvidenceFilePath = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="srcFolder"></param>
        /// <param name="extension"></param>
        /// <param name="distanceThreshold"></param>
        /// <param name="setenceDistance"></param>
        /// <param name="paragraphDistance"></param>
        /// <param name="evidenceFilePath"></param>
        public DocumentLabelInstruction(Guid componentID, Guid jobTicketID,
            string srcFolder, string extension, int distanceThreshold,
            int setenceDistance, int paragraphDistance, string evidenceFilePath) : base(componentID, jobTicketID)
        {
            this._SrcFolderPath = srcFolder;
            this._FileExtension = extension;
            this._DistanceThreshold = distanceThreshold;
            this._SetenceDistance = setenceDistance;
            this._ParagraphDistance = paragraphDistance;
            this._EvidenceFilePath = evidenceFilePath;
        }

        #endregion

        #region Overrides of InstructionBase

        /// <summary>
        /// serialize settings to xml
        /// the function below is mainly for storing into database as serialized form instead of string
        /// </summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SrcFolderPath",this.SrcFolderPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileExtension", this.FileExtension);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DistanceThreshold", this.DistanceThreshold.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SetenceDistance", this.SetenceDistance.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ParagraphDistance", this.ParagraphDistance.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "EvidenceFilePath", this.EvidenceFilePath);

            return xNode;
        }

        /// <summary>
        /// instantiated from xml
        /// "&lt;Instructions&gt;&lt;Instruction&gt;&lt;/Instruction&gt;&lt;/Instruction&gt;", must
        /// instantiate from first child of root node
        /// </summary>
        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                DocumentLabelInstruction instruction = new DocumentLabelInstruction();
                instruction.OwnerComponentID = new Guid(XmlDataUtil.GetAttributeValue(xNode, "ComponentID", Guid.NewGuid().ToString()));
                instruction.OwnerWorkerInstanceID = new Guid(XmlDataUtil.GetAttributeValue(xNode, "WorkerID", Guid.NewGuid().ToString()));
                instruction.SubmitTime = DateTime.Parse(XmlDataUtil.GetAttributeValue(xNode, "SubmitTime", TimeUtil.DbMinTime.ToString()));
                instruction.StartTime = DateTime.Parse(XmlDataUtil.GetAttributeValue(xNode, "StartTime", TimeUtil.DbMinTime.ToString()));

                instruction.SrcFolderPath = XmlDataUtil.GetAttributeValue(xNode, "SrcFolderPath", "");
                instruction.FileExtension = XmlDataUtil.GetAttributeValue(xNode, "FileExtension", "");
                instruction.DistanceThreshold =
                    int.Parse(XmlDataUtil.GetAttributeValue(xNode, "DistanceThreshold", "25"));
                instruction.SetenceDistance = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "SetenceDistance", "5"));
                instruction.ParagraphDistance =
                    int.Parse(XmlDataUtil.GetAttributeValue(xNode, "ParagraphDistance", "10"));
                instruction.EvidenceFilePath = XmlDataUtil.GetAttributeValue(xNode, "EvidenceFilePath", "");

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
